import SwiftUI
import Alamofire

// Book/fetchlook
struct ResponseData_FetchLook: Codable {
    let status: Bool
    let collection: [Book]
}

class BookLookFetcher: ObservableObject {
    @Published var publishedResult: BookCollection = BookCollection(collection: [DefaultBook])
        
    func fetch(query: String)
    {
        let requestURL = "http://62.234.45.120:1230/Book/fetchlook?query=" + query
        
        AF.request(requestURL, method: .get)
        .responseDecodable(of: ResponseData_FetchLook.self) { response in
            if let responseData = response.value {
                if responseData.status {
                    self.publishedResult.collection = responseData.collection
                }
            }
        }
    }
}

// Book/fetchsell
struct ResponseData_FetchSell: Codable {
    let status: Bool
    let description: String
    let collection: [Book_Sell]
}

class BookSellFetcher: ObservableObject {
    @Published var publishedResult: Book_SellCollection = Book_SellCollection(collection: [DefaultBook_Sell])
        
    func fetch(userId: String)
    {
        let requestURL = "http://62.234.45.120:1230/Book/fetchsell?userId=" + userId
        
        AF.request(requestURL, method: .get)
        .responseDecodable(of: ResponseData_FetchSell.self) { response in
            if let responseData = response.value {
                if responseData.status {
                    self.publishedResult.collection = responseData.collection
                }
            }
        }
    }
}

// Book/fetchcode
struct ResponseData_FetchCode: Codable {
    let status: Bool
    let description: String
    let bookName: String
    let author: String
    let course: String
}

class BookCodeFetecher: ObservableObject {
    @Published var publishedResult: (status: Bool, bookName: String, author: String, course: String) = (false, "", "", "")
    
    func fetch(code: String)
    {
        let requestURL = "http://62.234.45.120:1230/Book/fetchcode?code=" + code
        
        AF.request(requestURL, method: .get)
        .responseDecodable(of: ResponseData_FetchCode.self) { response in
            if let responseData = response.value {
                self.publishedResult.status = responseData.status
                self.publishedResult.bookName = responseData.bookName
                self.publishedResult.author = responseData.author
                self.publishedResult.course = responseData.course
            }
        }
    }
}

// Book/addbook
struct RequestData_AddBook: Codable {
    let userId: String
}

struct ResponseData_AddBook: Codable {
    let status: Bool
    let description: String
    let bookId: String
}

class BookAddBookRequester: ObservableObject {
    @Published var publishedResult: (status: Bool, bookId: String) = (false, "")
    
    func request(userId: String) {
        let requestData = RequestData_AddBook(userId: userId)
        
        let requestURL = "http://62.234.45.120:1230/Book/addbook"
        
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_AddBook.self) { response in
            if let responseData = response.value {
                self.publishedResult.status = responseData.status
                if responseData.status {
                    self.publishedResult.bookId = responseData.bookId
                }
            }
        }
    }
}

// Book/deletebook
struct RequestData_DeleteBook: Codable {
    let bookId: String
}

struct ResponseData_DeleteBook: Codable {
    let status: Bool
    let description: String
}

class BookDeleteBookRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: Bool = false
    
    func request_alert(_ requester: BookDeleteBookRequester, bookId: String) {
        showSellDeteleAlert(requester, bookId: bookId)
    }
    
    func request(bookId: String) {
        // 准备数据
        let requestData = RequestData_DeleteBook(bookId: bookId)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Book/deletebook"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_DeleteBook.self) { response in
            if let responseData = response.value {
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
}

// Book/modifybook
struct RequestData_ModifyBook: Codable {
    let bookId: String
    let bookName: String
    let tag: String
    let requirePrice: Int
    let author: String
    let course: String
    let description: String
}

struct ResponseData_ModifyBook: Codable {
    let status: Bool
    let description: String
}

class BookModifyBookRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: Bool = false
    
    func request(bookId: String, bookName: String, tag: String, requirePrice: String, author: String, course: String, description: String) {
        // 准备数据
        let requestData = RequestData_ModifyBook(bookId: bookId,
                                                 bookName: bookName,
                                                 tag: tag,
                                                 requirePrice: Int(Double(requirePrice[requirePrice.index(after: requirePrice.startIndex)..<requirePrice.endIndex])! * 100),
                                                 author: author,
                                                 course: course,
                                                 description: description)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Book/modifybook"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_ModifyBook.self) { response in
            if let responseData = response.value {
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
}

// Book/modifystate
struct RequestData_ModifyState: Codable {
    let bookId: String
    let state: Int
}

struct ResponseData_ModifyState: Codable {
    let status: Bool
    let description: String
}

class BookModifyStateRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: Bool = false
    
    func request(bookId: String, state: Int) {
        // 准备数据
        let requestData = RequestData_ModifyState(bookId: bookId,
                                                  state: state)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Book/modifystate"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_ModifyState.self) { response in
            if let responseData = response.value {
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
}

// Book/lazydeleteimage
struct RequestData_LazyDeleteImage: Codable {
    let imageId: String
}

struct ResponseData_LazyDeleteImage: Codable {
    let status: Bool
    let description: String
    let imageId: String
}

class BookLazyDeleteImageRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: (status: Bool, imageId: String) = (false, "")
    
    func request(imageId: String) {
        // 准备数据
        let requestData = RequestData_LazyDeleteImage(imageId: imageId)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Book/lazydeleteimage"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_LazyDeleteImage.self) { response in
            if let responseData = response.value {
                self.publishedResult.status = responseData.status
                self.publishedResult.imageId = responseData.imageId
                self.requestStatus = .finish
            }
        }
    }
}
